package com.tfswue.dyq.service;

import com.tfswue.dyq.client.BookClient;
import com.tfswue.dyq.entity.BorrowRecord;
import com.tfswue.dyq.mapper.BorrowRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.awt.print.Book;

@Service
public class BorrowService {
    private final BorrowRecordMapper borrowRecordMapper;
    private final BookClient bookClient;

    public BorrowService(BorrowRecordMapper borrowRecordMapper, BookClient bookClient) {
        this.borrowRecordMapper = borrowRecordMapper;
        this.bookClient = bookClient;
    }

    public String borrowBook(Long userId, Long bookId) {
        // 调用图书服务验证图书
        Book book = bookClient.getBookById(bookId);
        if (book == null) {
            return "图书不存在";
        }

        // 创建借阅记录
        BorrowRecord record = new BorrowRecord();
        record.setUserId(userId);
        record.setBookId(bookId);
        borrowRecordMapper.insert(record);

        return "借阅成功";
    }
}
